﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SevenZip;
using System.IO;

namespace Steam_Backup
{
    public partial class FrmBackup : Form
    {
        List<GameEntry> m_gameEntries = new List<GameEntry>();
        Settings m_settings = new Settings();
        double m_dCurrentProgress = 0.0;

        public FrmBackup(List<GameEntry> a_gameEntries, Settings a_settings)
        {
            InitializeComponent();
            m_settings = a_settings;
            m_gameEntries = a_gameEntries;
        }

        private void FrmBackup_Load(object sender, EventArgs e)
        {
            SevenZipCompressor compressor = new SevenZipCompressor();
            compressor.ArchiveFormat = OutArchiveFormat.SevenZip;
            compressor.Compressing += new EventHandler<ProgressEventArgs>(OnCompressing);
            compressor.CompressionLevel = CompressionLevel.Ultra;

            
            foreach (GameEntry entry in m_gameEntries)
            {
                string szBackupPath = Path.Combine(m_settings.BackupLocation, entry.GameTitle);

                if (!Directory.Exists(szBackupPath))
                {
                    try
                    {
                        Directory.CreateDirectory(szBackupPath);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

                Dictionary<string, string> fileDict = new Dictionary<string, string>();
                foreach (string file in entry.MainFiles)
                {
                    FileInfo fi = new FileInfo(file);
                    fileDict.Add(fi.Name, fi.FullName);
                }
                
                DirectoryInfo diGameFolder = new DirectoryInfo (Path.Combine(Utilities.GetSteamPath(), 
                                                                             Utilities.s_szSteamApps, 
                                                                             Utilities.s_szCommon, 
                                                                             entry.GameFolder));

                try
                {
                    foreach (FileInfo fi in diGameFolder.GetFiles("*.*", SearchOption.AllDirectories))
                    {
                        string szRelativeFile = fi.FullName.Replace(diGameFolder.FullName + "\\", string.Empty);
                        string key = Path.Combine(Utilities.s_szCommon, entry.GameFolder, szRelativeFile);
                        fileDict.Add(key, fi.FullName);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                //DirectoryInfo dir = new DirectoryInfo(entry.GameFolder);
                //foreach(FileInfo fi in dir.GetFiles("*.*", SearchOption.AllDirectories))
                //{
                    //fileDict.Add(Path.Combine(s_szCommon, fi.FullName), Path.Combine(m_settings.SteamLocation, Utilities.s_szSteamApps, file));
                //}


                compressor.CompressFileDictionary(fileDict, Path.Combine(szBackupPath, entry.GameTitle+ ".7z"));
            }
            //foreach(string file in )
            
            //foreach(FileInfo fi in 
        }

        void OnCompressing(object sender, ProgressEventArgs e)
        {
            m_dCurrentProgress += e.PercentDelta;
            //byte euh = e.PercentDelta;// / e.PercentDone;
            prgCurrentProgress.Value = (int)m_dCurrentProgress;
        }
    }
}
